package net.runman.config;


import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import net.runman.interceptors.TestInterceptors;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

/**
 * Web相关配置
 * @author L.cm
 * @email: 596392912@qq.com
 * @site: http://www.dreamlu.net
 * @date 2015年5月3日下午12:52:33
 */
@Configuration
//@EnableWebMvc // 为从xml中配置，本例采用全注解配置，故注释掉
public class WebMvcConfig extends WebMvcConfigurerAdapter {

	// UTF8字符集
	private static final Charset UTF8 = Charset.forName("UTF-8");

	// druid监控用户名
	@Value("${druid.username}")
	private String druidUsername;
	// druid监控密码
	@Value("${druid.password}")
	private String druidPassword;

	/**
	 * 注册druid监控Servlet
	 * @return ServletRegistrationBean
	 */
	@Bean
	public ServletRegistrationBean druidStatViewServletBean(){
		ServletRegistrationBean druidStatViewServletBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
		druidStatViewServletBean.addInitParameter("loginUsername", druidUsername);
		druidStatViewServletBean.addInitParameter("loginPassword", druidPassword);
		return druidStatViewServletBean;
	}

	/**
	 * 注册druid监控Filter
	 * @return FilterRegistrationBean
	 */
	@Bean
	public FilterRegistrationBean druidWebStatFilterBean(){
		FilterRegistrationBean druidWebStatFilterBean = new FilterRegistrationBean();
		druidWebStatFilterBean.setFilter(new WebStatFilter());
		druidWebStatFilterBean.addUrlPatterns("/*");
		druidWebStatFilterBean.addInitParameter("exclusions", "/static/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
		druidWebStatFilterBean.addInitParameter("sessionStatEnable", "false");
		return druidWebStatFilterBean;
	}

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		// Set HTTP Message converter using a JSON implementation.
		MappingJackson2HttpMessageConverter jsonMessageConverter = new MappingJackson2HttpMessageConverter();
		// Add supported media type returned by BI API.
		List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
		supportedMediaTypes.add(new MediaType("text", "plain", UTF8));
		supportedMediaTypes.add(new MediaType("application", "json", UTF8));
		jsonMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
		converters.add(jsonMessageConverter);
	}

	/**
	 * 直接访问jsp的路由
	 */
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/test.html").setViewName("html/test");
		registry.addViewController("/404.html").setViewName("html/404");
	}

	/**
	 * 配置资源文件路由
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/static/**").addResourceLocations("/static/");
	}

	/**
	 * 配置拦截器
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new TestInterceptors()).excludePathPatterns("/static/**").addPathPatterns("/**");
	}

}
